package controls;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Map;

import main.Application;

import org.xml.sax.SAXException;

import tablemanager.TableManager;
import browser.Browser;
import browser.filter.ANDFilterNode;
import browser.filter.BrowserFilter;
import browser.filter.FilterLeaf;
import exception.queryBuilder.AliasNotFound;

public class InnerBrowser extends Browser 
{	
	private static final long serialVersionUID = 9089542877993377483L;
	public static final String INNER_BROWSER_PREFIX = "inner_";
	private String parentName;
	private BrowserFilter bf;
	Map<String,String> keyVals;//klucz(nazwa jak w bazie) i wartosc jak w edytce w chwili woloania(wartosci kluczy nie moga sie zmienic)
	public InnerBrowser(String tabName, int width, int height,String parentName) throws AliasNotFound, SQLException, SAXException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
	{
		super(tabName, width, height);
		this.parentName=parentName;
		keyVals = new Hashtable<String, String>();
	}
	public void setParent(Map<String,String> record) throws SQLException, AliasNotFound
	{
		TableManager parentTM = Application.getInstance().getTableManager(parentName);
		String[] keys= parentTM.getKeysNames();
		bf = new BrowserFilter();
		ANDFilterNode node = new ANDFilterNode();		
		for(String key : keys)
		{
			FilterLeaf leaf= new FilterLeaf(key,record.get(key),false); 
			keyVals.put(leaf.getLeafName(),leaf.getLeafValue());
			node.addFilterElement(leaf);
		}
		bf.setRoot(node);
		setFilter(bf);
		bufferedMode=true;
		insertRecEdRunner.setRecord(keyVals);
		insertRecEdRunner.setRunFromInnerBrowser(true);
	}	
	public void setEnabled(boolean enabled)
	{
		insertButton.setEnabled(enabled);
		modifyButton.setEnabled(enabled);
		deleteButton.setEnabled(enabled);
	}
}
